﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using System.Data;

public partial class IFRS_SEC02_1S : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            this.hidRoleGroupID.Value = Request.QueryString["id"].ToString();
            this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox cbgvGroup = (CheckBox)e.Row.FindControl("cbgvGroup");
            CheckBox cbgvView = (CheckBox)e.Row.FindControl("cbgvView");
            CheckBox cbgvAdd = (CheckBox)e.Row.FindControl("cbgvAdd");
            CheckBox cbgvEdit = (CheckBox)e.Row.FindControl("cbgvEdit");
            CheckBox cbgvDelete = (CheckBox)e.Row.FindControl("cbgvDelete");
            CheckBox cbgvPrint = (CheckBox)e.Row.FindControl("cbgvPrint");
            DataRowView drv = (e.Row.DataItem as DataRowView);
            if ((bool)drv.Row["IsUsed"])
            {
                cbgvGroup.Checked = true;
                cbgvView.Enabled = true;
                cbgvAdd.Enabled = true;
                cbgvEdit.Enabled = true;
                cbgvDelete.Enabled = true;
                cbgvPrint.Enabled = true;
            }
            else
            {
                cbgvGroup.Checked = false;
                cbgvView.Enabled = false;
                cbgvAdd.Enabled = false;
                cbgvEdit.Enabled = false;
                cbgvDelete.Enabled = false;
                cbgvPrint.Enabled = false;
            }
            if ((bool)drv.Row["IsView"])
            {
                cbgvView.Checked = true;
            }
            else
            {
                cbgvView.Checked = false;
            }
            if ((bool)drv.Row["IsAdd"])
            {
                cbgvAdd.Checked = true;
            }
            else
            {
                cbgvAdd.Checked = false;
            }
            if ((bool)drv.Row["IsEdit"])
            {
                cbgvEdit.Checked = true;
            }
            else
            {
                cbgvEdit.Checked = false;
            }
            if ((bool)drv.Row["IsDelete"])
            {
                cbgvDelete.Checked = true;
            }
            else
            {
                cbgvDelete.Checked = false;
            }
            if ((bool)drv.Row["IsPrint"])
            {
                cbgvPrint.Checked = true;
            }
            else
            {
                cbgvPrint.Checked = false;
            }
          
            //e.Row.SetRowAttribute(drv.Row["RoleGroupID"].ToString());
            e.Row.SetRowAttribute("RoleGroupMenuID");
        }
    }
    protected void cbgvGroup_CheckedChanged(object sender, EventArgs e)
    {
        GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
        int index = row.RowIndex;
        CheckBox cbgvGroup = (CheckBox)gvSearch.Rows[index].FindControl("cbgvGroup");
        CheckBox cbgvView = (CheckBox)gvSearch.Rows[index].FindControl("cbgvView");
        CheckBox cbgvAdd = (CheckBox)gvSearch.Rows[index].FindControl("cbgvAdd");
        CheckBox cbgvEdit = (CheckBox)gvSearch.Rows[index].FindControl("cbgvEdit");
        CheckBox cbgvDelete = (CheckBox)gvSearch.Rows[index].FindControl("cbgvDelete");
        CheckBox cbgvPrint = (CheckBox)gvSearch.Rows[index].FindControl("cbgvPrint");
        if (cbgvGroup.Checked)
        {
            cbgvView.Enabled = true;
            cbgvAdd.Enabled = true;
            cbgvEdit.Enabled = true;
            cbgvDelete.Enabled = true;
            cbgvPrint.Enabled = true;
        }
        else 
        {
            cbgvView.Enabled = false;
            cbgvAdd.Enabled = false;
            cbgvEdit.Enabled = false;
            cbgvDelete.Enabled = false;
            cbgvPrint.Enabled = false;
        }


    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/SEC01_1S.aspx");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        bool SuccessFlag = true;
        
        foreach (GridViewRow row in gvSearch.Rows)
        {
            bool UseFlag = true;
            CheckBox cbgvGroup = (CheckBox)row.FindControl("cbgvGroup");
            if (cbgvGroup.Checked)
            {
                int RoleGroupMenuID = Convert.ToInt32(row.Attributes["RoleGroupMenuID"].ToString());
                CheckBox cbgvView = (CheckBox)row.FindControl("cbgvView");
                CheckBox cbgvAdd = (CheckBox)row.FindControl("cbgvAdd");
                CheckBox cbgvEdit = (CheckBox)row.FindControl("cbgvEdit");
                CheckBox cbgvDelete = (CheckBox)row.FindControl("cbgvDelete");
                CheckBox cbgvPrint = (CheckBox)row.FindControl("cbgvPrint");

                if (cbgvView.Checked == false && cbgvAdd.Checked == false && cbgvEdit.Checked == false && cbgvDelete.Checked == false && cbgvPrint.Checked == false)
                {
                    UseFlag = false;
                }

                var vROLEGROUP = new ROLEGROUP();
                int iID = vROLEGROUP.UPDATE_ROLEGROUPMENU(RoleGroupMenuID, UseFlag, cbgvView.Checked, cbgvAdd.Checked,
                                            cbgvEdit.Checked,cbgvDelete.Checked,cbgvPrint.Checked, DateTime.Now, SessionManager.IFRS_EMPLOYEE_ID,
                                            SessionManager.IFRS_COST_CENTER, SessionManager.IFRS_BRANCH, DateTime.Now, SessionManager.IFRS_EMPLOYEE_ID,
                                            SessionManager.IFRS_COST_CENTER, SessionManager.IFRS_BRANCH);
                if (iID <= 0)
                {
                    SuccessFlag = false;
                }
            }
        }
        if (SuccessFlag)
        {
            this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/SEC02_1S.aspx?&id={0}", hidRoleGroupID.Value.AsInt32())));
        }
        else
        {
            Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
        }
    }
    #endregion
    #region "---------- Methods ----------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.SEC02_1S;
    }
    private void BindData(int ID)
    {
        //Bind Header
        var vROLEGROUP = new ROLEGROUP();
        DataTable dtRoleGroup = vROLEGROUP.SEARCH_BYID(ID);
        List<RoleGroupU> RoleGroup = new List<RoleGroupU>();
        ConvertHelper.ConvertDataTableToObject(dtRoleGroup, RoleGroup);
        if (dtRoleGroup.Rows.Count > 0)
        {
            txtROLE_GROUP_NAME.Text = RoleGroup[0].RoleGroupName.ToString();//dtRoleGroup.Rows[0]["RoleGroupName"].ToString();

            if (RoleGroup[0].AllBranch.ToString().ToLower() == "true")
            {
                cbJOB.Checked = true;
            }
            if (RoleGroup[0].IsActive.ToString().ToLower() == "true")
            {
                rbSTATUS_1.Checked = true;
            }
            else
            {
                rbSTATUS_2.Checked = true;
            }

        }

        //Bind Grid
        DataTable dtRoleGroupMenu = vROLEGROUP.SEARCH_ROLEGROUPMENU_BYID(ID);
        if (dtRoleGroupMenu.Rows.Count > 0)
        {
            gvSearch.DataSource = dtRoleGroupMenu;
            gvSearch.DataBind();

        }
    }


    #endregion


   
}